{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Checking whether there is an H2O instance running at http://localhost:54321..... not found.\n",
      "Attempting to start a local H2O server...\n",
      "  Java Version: openjdk version \"1.8.0_181\"; OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-2~deb9u1-b13); OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)\n",
      "  Starting server from /opt/conda/lib/python2.7/site-packages/h2o/backend/bin/h2o.jar\n",
      "  Ice root: /tmp/tmpz8qTmm\n",
      "  JVM stdout: /tmp/tmpz8qTmm/h2o_unknownUser_started_from_python.out\n",
      "  JVM stderr: /tmp/tmpz8qTmm/h2o_unknownUser_started_from_python.err\n",
      "  Server is running at http://127.0.0.1:54321\n",
      "Connecting to H2O server at http://127.0.0.1:54321... successful.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div style=\"overflow:auto\"><table style=\"width:50%\"><tr><td>H2O cluster uptime:</td>\n",
       "<td>01 secs</td></tr>\n",
       "<tr><td>H2O cluster timezone:</td>\n",
       "<td>Etc/UTC</td></tr>\n",
       "<tr><td>H2O data parsing timezone:</td>\n",
       "<td>UTC</td></tr>\n",
       "<tr><td>H2O cluster version:</td>\n",
       "<td>3.22.1.1</td></tr>\n",
       "<tr><td>H2O cluster version age:</td>\n",
       "<td>23 days </td></tr>\n",
       "<tr><td>H2O cluster name:</td>\n",
       "<td>H2O_from_python_unknownUser_ukj9f9</td></tr>\n",
       "<tr><td>H2O cluster total nodes:</td>\n",
       "<td>1</td></tr>\n",
       "<tr><td>H2O cluster free memory:</td>\n",
       "<td>3.042 Gb</td></tr>\n",
       "<tr><td>H2O cluster total cores:</td>\n",
       "<td>7</td></tr>\n",
       "<tr><td>H2O cluster allowed cores:</td>\n",
       "<td>7</td></tr>\n",
       "<tr><td>H2O cluster status:</td>\n",
       "<td>accepting new members, healthy</td></tr>\n",
       "<tr><td>H2O connection url:</td>\n",
       "<td>http://127.0.0.1:54321</td></tr>\n",
       "<tr><td>H2O connection proxy:</td>\n",
       "<td>None</td></tr>\n",
       "<tr><td>H2O internal security:</td>\n",
       "<td>False</td></tr>\n",
       "<tr><td>H2O API Extensions:</td>\n",
       "<td>XGBoost, Algos, AutoML, Core V3, Core V4</td></tr>\n",
       "<tr><td>Python version:</td>\n",
       "<td>2.7.15 final</td></tr></table></div>"
      ],
      "text/plain": [
       "--------------------------  ----------------------------------------\n",
       "H2O cluster uptime:         01 secs\n",
       "H2O cluster timezone:       Etc/UTC\n",
       "H2O data parsing timezone:  UTC\n",
       "H2O cluster version:        3.22.1.1\n",
       "H2O cluster version age:    23 days\n",
       "H2O cluster name:           H2O_from_python_unknownUser_ukj9f9\n",
       "H2O cluster total nodes:    1\n",
       "H2O cluster free memory:    3.042 Gb\n",
       "H2O cluster total cores:    7\n",
       "H2O cluster allowed cores:  7\n",
       "H2O cluster status:         accepting new members, healthy\n",
       "H2O connection url:         http://127.0.0.1:54321\n",
       "H2O connection proxy:\n",
       "H2O internal security:      False\n",
       "H2O API Extensions:         XGBoost, Algos, AutoML, Core V3, Core V4\n",
       "Python version:             2.7.15 final\n",
       "--------------------------  ----------------------------------------"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parse progress: |█████████████████████████████████████████████████████████| 100%\n"
     ]
    }
   ],
   "source": [
    "import h2o\n",
    "import random\n",
    "import mlflow\n",
    "import mlflow.h2o\n",
    "from h2o.estimators.random_forest import H2ORandomForestEstimator\n",
    "h2o.init()\n",
    "\n",
    "wine = h2o.import_file(path=\"wine-quality.csv\")\n",
    "r = wine['quality'].runif()\n",
    "train = wine[r  < 0.7]\n",
    "test  = wine[0.3 <= r]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_random_forest(ntrees):\n",
    "    with mlflow.start_run():\n",
    "        rf = H2ORandomForestEstimator(ntrees=ntrees)\n",
    "        train_cols = [n for n in wine.col_names if n != \"quality\"]\n",
    "        rf.train(train_cols, \"quality\", training_frame=train, validation_frame=test)\n",
    "        \n",
    "        mlflow.log_param(\"ntrees\", ntrees)\n",
    "        \n",
    "        mlflow.log_metric(\"rmse\", rf.rmse())\n",
    "        mlflow.log_metric(\"r2\", rf.r2())\n",
    "        mlflow.log_metric(\"mae\", rf.mae())\n",
    "        \n",
    "        mlflow.h2o.log_model(rf, \"model\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "drf Model Build progress: |███████████████████████████████████████████████| 100%\n",
      "drf Model Build progress: |███████████████████████████████████████████████| 100%\n",
      "drf Model Build progress: |███████████████████████████████████████████████| 100%\n",
      "drf Model Build progress: |███████████████████████████████████████████████| 100%\n",
      "drf Model Build progress: |███████████████████████████████████████████████| 100%\n"
     ]
    }
   ],
   "source": [
    "for ntrees in [10, 20, 50, 100, 200]:\n",
    "    train_random_forest(ntrees)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function yaml.safe_dump>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yaml.safe_dump"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
